package com.yanqu.road.dao.impl.activity.horserun.cross;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.horserun.cross.CrossHorseRunUnionData;
import org.apache.mina.util.ConcurrentHashSet;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossHorseRunUnionDataDaoImpl extends TempDataObjectDao<CrossHorseRunUnionData> {
    public static String getCrossHorseRunUnionDataSql = "select * from `t_c_horse_run_union_data` where `activityId`=? and `groupId`=?";
    public Map<String, CrossHorseRunUnionData> getCrossHorseRunUnionData(int activityId,int groupId) {
        Map<String, CrossHorseRunUnionData> unionDataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCrossHorseRunUnionDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossHorseRunUnionData parse = parse(rs);
                unionDataMap.put(parse.getUnionId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return unionDataMap;
    }

    public static String updateCrossHorseRunUnionDataSql = "update `t_c_horse_run_union_data` set unionLevel=?,totalGrade=?,totalAchievementScore=?,joinMember=? where activityId=? and groupId=? and unionId=?";
    @Override
    protected boolean updateData(CrossHorseRunUnionData crossHorseRunUnionData) {
        crossHorseRunUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUnionData.getUnionLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunUnionData.getTotalGrade()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUnionData.getTotalAchievementScore())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUnionData.getJoinMember())));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUnionData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossHorseRunUnionData.getUnionId()));
        if (getDb().executeNoneQuery(updateCrossHorseRunUnionDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossHorseRunUnionDataSql = "insert into `t_c_horse_run_union_data`(activityId,groupId,unionId,unionLevel,totalGrade,totalAchievementScore,joinMember) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossHorseRunUnionData crossHorseRunUnionData) {
        crossHorseRunUnionData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUnionData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUnionData.getGroupId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossHorseRunUnionData.getUnionId()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHorseRunUnionData.getUnionLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHorseRunUnionData.getTotalGrade()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUnionData.getTotalAchievementScore())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(crossHorseRunUnionData.getJoinMember())));
        if (getDb().executeNoneQuery(insertCrossHorseRunUnionDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossHorseRunUnionData parseData(ResultSet rs) throws SQLException {
        CrossHorseRunUnionData crossHorseRunUnionData = new CrossHorseRunUnionData();
        crossHorseRunUnionData.setActivityId(rs.getInt("activityId"));
        crossHorseRunUnionData.setGroupId(rs.getInt("groupId"));
        crossHorseRunUnionData.setUnionId(rs.getString("unionId"));
        crossHorseRunUnionData.setUnionLevel(rs.getInt("unionLevel"));
        crossHorseRunUnionData.setTotalGrade(rs.getLong("totalGrade"));
        crossHorseRunUnionData.setTotalAchievementScore(JSON.parseObject(rs.getString("totalAchievementScore"), new TypeReference<ConcurrentHashMap<Integer, Long>>() {}));
        crossHorseRunUnionData.setJoinMember(JSON.parseObject(rs.getString("joinMember"), new TypeReference<ConcurrentHashSet<Long>>(){}));

        return crossHorseRunUnionData;
    }

}
